/**
 * 初始化入口封装
 * @author hongwei 2018-09-19
 */
(function (window) {
    'use strict';
    var initPlat = function (params) {
        $.extend(this.params, params);
        this._init()
    };

    initPlat.prototype = {

        params : {
            
        }
        
        ,_init : function () {
            var that = this;
            if(typeof(api) === 'undefined'){
                console.log('该插件只能在apicloud下运行');
            } else {
                that.jpush = api.require('ajpush');
                that.welcomeInit();
                //that.netOffline();
                that.initJPush(that.jpush);
                that.getId(that.jpush);
                that.setBadgeZero(that.jpush);
                that.listenIntent();
                that.listenPause();
            }
        }

        //step1:欢迎页
        ,welcomeInit : function () {
            var that = this;
            // var welcomeShowed = $api.getStorage('welcome');
            var welcomeShowed = 1;
            if (welcomeShowed) {
                //验证是否登录
                if(!that.isLogin()){
                    //验证是否登录
                    api.openWin({
                        name: 'login',
                        url: './html/login/login.html',
                        slidBackEnabled:false
                    });
                }else{
                    //有用户登录信息-跳转首页
                    api.openWin({
                        name: 'main',
                        url: './html/main.html',
                        slidBackEnabled:false
                    });
                }
            } else {
                api.openWin({
                    name: 'welcome',
                    url: './html/welcome/welcome.html',
                    slidBackEnabled:false
                });
            }
        }

        //step2:断网情况下的判断
        ,netOffline : function () {
            api.addEventListener({
                name: 'offline'
            }, function(ret, err) {
                //alert('断网了，请重新连接网络！');
            });
        }

        //step3:推送-初始化推送
        ,initJPush : function (jpush){
            jpush.init(function(ret, err){
                if(ret && ret.status){
                    //console.log(JSON.stringify(ret));
                    //console.log('初始化推送成功!');
                }else{
                    //console.log('初始化推送失败!');
                }
            });
        }

        //step4:推送-获取getRegistrationId
        ,getId : function (jpush){
            jpush.getRegistrationId(function(ret, err){
                //console.log(JSON.stringify(ret));
            });
        }

        //step5:清空应用图标右上角数字，只iOS有效setBadge
        ,setBadgeZero : function (jpush) {
            jpush.setBadge({
                badge:0
            });
            //console.log('图标角标清空，请查看图标！')
        }

        //step6:监听到推送信息
        ,listenIntent : function () {
            api.addEventListener({name: 'appintent'}, function (ret, err) {
                console.log('通知被点击，收到数据：\n' + JSON.stringify(ret));//监听通知被点击后收到的数据
                if (ret && ret.appParam.ajpush) {
                    //var ajpush = ret.appParam.ajpush;
                    // console.log(ajpush);
                    // console.log(ajpush.id);
                    // console.log(ajpush.title);
                    // console.log(ajpush.content);
                    // console.log(JSON.stringify(ajpush.extra));
                }
            });
        }
        
        //step7:监听进入应用后台
        ,listenPause : function () {
            api.addEventListener({
                name:'pause'
            }, function(ret, err){
                console.log('应用进入后台');
            });
        }

        //检测是否登录
        ,isLogin :function () {
            var isloginToken = $api.getStorage('token');
            if (!isloginToken) {
                return 0;
            } else {
                return 1;
            }
        }

    };
    window.initPlat = initPlat;
})(window);